package com.yima.baseproj.module.sys.role;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yima.baseproj.aop.log.Logging;
import com.yima.baseproj.config.exception.type.ApiException;
import com.yima.baseproj.module.config.BaseController;
import com.yima.baseproj.module.config.utils.PageUtil;
import com.yima.baseproj.module.config.utils.PageVo;
import com.yima.baseproj.module.config.utils.ResultVo;
import com.yima.baseproj.module.config.utils.ResultUtil;
import com.yima.baseproj.module.sys.role.menu.SysRoleMenuService;
import com.yima.baseproj.module.sys.role.model.SysRoleAddParamModel;
import com.yima.baseproj.module.sys.role.model.SysRoleEditParamModel;
import com.yima.baseproj.module.sys.role.model.RoleAddOrEditModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Author 一码
 * @createDate 2023/4/1 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "角色管理",value = "角色吧")
@RequestMapping("/api/sys/role")
@RestController
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @PreAuthorize("hasAuthority('sys:role:findAllByPage')")
    @ApiOperation(value = "查询分页")
    @GetMapping("/findAllByPage")
    public ResultVo getSysRoleList(RoleAddOrEditModel role, PageVo page) {
        Page<Map> pageData = sysRoleService.findAllByPage(PageUtil.initMpPage(page),role);
        for (Map record : pageData.getRecords()) {
            List<String> sysRoleMenuEntity = sysRoleMenuService.findMenuIdListByRoleId(record.get("id").toString());
            record.put("menuIdList",sysRoleMenuEntity);
        }
        return ResultUtil.success(pageData);
    }

    @PreAuthorize("hasAuthority('sys:role:findAllByList')")
    @ApiOperation("查询列表")
    @GetMapping("/findAllByList")
    public ResultVo getRole(RoleAddOrEditModel role) {
        List<Map> roleList = sysRoleService.findAllByList(role);
        return ResultUtil.success(roleList);
    }

    @Logging(tags = "角色管理",value = "添加")
    @PreAuthorize("hasAuthority('sys:role:add')")
    @ApiOperation("添加")
    @PostMapping("/add")
    public void addRole(@RequestBody @Validated SysRoleAddParamModel paramModel) {
        SysRoleEntity exist = sysRoleService.checkRoleNameUnique(paramModel.getName());
        if (!Objects.isNull(exist)) {
            throw new ApiException("角色名称已存在");
        }
        sysRoleService.add(paramModel);
    }

    @Logging(tags = "角色管理",value = "修改")
    @PreAuthorize("hasAuthority('sys:role:edit')")
    @ApiOperation("修改")
    @PostMapping("/edit")
    public void editRole(@RequestBody @Validated SysRoleEditParamModel role) {
        SysRoleEntity exist = sysRoleService.checkRoleNameUnique(role.getName());
        if (!Objects.isNull(exist) && !exist.getId().equals(role.getId())) {
            throw new ApiException("角色名称已存在");
        }
        sysRoleService.editRole(role);
    }

    @Logging(tags = "角色管理",value = "删除")
    @PreAuthorize("hasAuthority('sys:role:delByIds')")
    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delByIds(@PathVariable String[] ids) {
        sysRoleService.deleteRole(ids);
    }
}
